<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="110px">
            <el-form-item label="消耗日期">
                <el-date-picker
                    v-model="dateRange"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :default-time="['00:00:00', '23:59:59']"
                    align="right">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-input v-model="queryParams.mccid_fatname" placeholder="请输入管家账户名称/ID" clearable style="width: 160px" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-select v-model="queryParams.status" placeholder="请选择数据状态" clearable style="width: 160px">
                    <el-option label="未冻结" value="1" />
                    <el-option label="已冻结" value="0" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row class="mb20">
            <el-col class="bg-bule" :span="10">
                <el-upload class="upload-btn" :action="uploadFileUrl" :headers = "uploadHeader" :on-success="filesSuccess">
                    <el-button type="primary" size="mini">导入</el-button>
                </el-upload>
                <el-button size="mini" @click="checkoutExcel">模板下载</el-button>
            </el-col>
        </el-row>
      
        <el-table v-loading="loading" :data="orderList">
            <el-table-column label="媒体消耗日期" align="center" prop="consumption_date" width="120" />
            <el-table-column label="一级媒体" align="center" prop="first_media" />
            <el-table-column label="管家账户ID" align="center" prop="mccid" width="120" />
            <el-table-column label="管家账户名称" align="center" prop="fatname" :show-overflow-tooltip="true" />
            <el-table-column label="媒体账面消耗" align="center" prop="consumption_book"  width="150" />
            <el-table-column label="媒体现金消耗" align="center" prop="consumption_cash"  width="150" />
            <el-table-column label="加微人数" align="center" prop="wechat_num" />
            <el-table-column label="加微现金成本" align="center" prop="wechat_cash_cost"  width="150" />
            <el-table-column label="账户余额（统计日期）" align="center" prop="account_balance"  width="160" />
            <el-table-column label="数据状态" align="center" prop="status_title" />
            <el-table-column label="最近修改人" align="center" prop="real_name"  width="120" />
            <el-table-column label="最近修改时间" align="center" prop="update_time"  width="160" />
            <el-table-column label="操作" align="center" >
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" @click="detele(scope.row.id)" :disabled="scope.row.status == 0">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.page"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
    </div>
</template>

<script>
import { lunchOrder, lunchDelete, getTemplate } from "@/api/financial/adver";
import { getToken } from '@/utils/auth'

export default {
    data() {
        return {
            loading: true,	// 遮罩层
            queryParams: { 	// 查询参数
                page: 1,
                pageSize: 20,
            },
            dateRange: [], 
            orderList:[],
            total:0,
            test:'https://test-api.huashouren.com/template/lunch_order.xlsx',
            uploadHeader:{},
            uploadFileUrl: process.env.VUE_APP_BASE_API + "/financial/lunchOrder/importFile"
        };
    },
    created() {
        this.uploadHeader.token = getToken()
        this.getList();
    },
    methods: {
        getList() {
            this.loading = false;
            //时间处理
            this.dateRange = Array.isArray(this.dateRange) ? this.dateRange : [];
            this.queryParams.consumption_date_start = this.dateRange[0];
            this.queryParams.consumption_date_end = this.dateRange[1];

            lunchOrder(this.queryParams).then(res => {
                let data = res.data.data
                let stat = res.data.statistics
                let title = {fatname:'总计'}
                let sum = {...title, ...stat}
                data.unshift(sum)
                this.orderList = data
                this.total = res.data.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
         /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                page: 1,
                pageSize: 20,
            },
            this.daterange = []
            this.handleQuery();
        },
        filesSuccess(res, file) {
            if(res.code == -1){
                this.$modal.msgError(res.msg);
            }else{
                this.getList()
                this.$modal.msgSuccess(res.msg);
            }
        },
        detele(id){
            this.$modal.confirm('是否确认删除？').then(function() {
                return  lunchDelete({id:id})
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
        },
        checkoutExcel(){
            getTemplate().then(res => {
                const fileUrl = res.data.lunch_order
                window.open(fileUrl)
            });
        },
    }
};
</script>

<style scoped>
  .upload-btn{display: inline-block;margin: 0 10px;}
</style>

<style>
  .upload-btn .el-upload-list{display: none;}
</style>
